const assert = require("assert");
const Redis = require("../src/redis.js");

describe("redis.js", () => {
  before(() => {
    console.log("init");
    Redis.init("127.0.0.1", "default", "KS5UggH4LNyyLBdr");
  });
  describe("getInstance()", () => {
    it("connect", async () => {
      try {
        let result = await Redis.exec("get", "test");
        console.log(result);
        console.log("Connection has been established successfully.");
        assert.ok(true);
      } catch (error) {
        console.error("Unable to connect to the redis:", error);
        assert.ok(false);
      }
    });
    it("set", async () => {
      try {
        await Redis.exec("set", "test", "OK");
        let reply = await Redis.exec("get", "test");
        console.log(reply);
        assert.ok(true);
      } catch (error) {
        assert.ok(false);
      }
    });
    it("set ex", async () => {
      try {
        await Redis.exec("setex", "test", "100", "OK");
        let reply = await Redis.exec("get", "test");
        console.log(reply);
        assert.ok(true);
      } catch (error) {
        console.log(error);
        assert.ok(false);
      }
    });
  });
  describe("formatKey()", () => {
    it("no params", async () => {
      let result = await Redis.formatKey("order", {});
      assert(result == "order");
    });
    it("has params", async () => {
      let result = await Redis.formatKey("order", { code: "123" });
      console.log(result);
      assert(result == "order:code:123");
    });
  });
});
